/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef NATIVE_XCOMPONENT_PLUGIN_RENDER_H
#define NATIVE_XCOMPONENT_PLUGIN_RENDER_H

#include <ace/xcomponent/native_interface_xcomponent.h>
#include <multimedia/image_framework/image_pixel_map_mdk.h>
#include <cstdint>
#include <napi/native_api.h>
#include <string>
#include <unordered_map>
#include <native_drawing/drawing_types.h>
#include <native_window/external_window.h>

namespace NativeXComponentSample {
class PluginRender {
public:
    PluginRender() = default;
    ~PluginRender();
    static PluginRender* GetInstance(OH_NativeXComponent* component);
    static void Release(OH_NativeXComponent* component);
    void Export(napi_env env, napi_value exports);
    void OnSurfaceChanged(OH_NativeXComponent* component, void* window);
    void OnTouchEvent(OH_NativeXComponent* component, void* window);
    void OnMouseEvent(OH_NativeXComponent* component, void* window);
    void OnHoverEvent(OH_NativeXComponent* component, bool isHover);
    void OnFocusEvent(OH_NativeXComponent* component, void* window);
    void OnBlurEvent(OH_NativeXComponent* component, void* window);
    void OnKeyEvent(OH_NativeXComponent* component, void* window);
    void RegisterCallback(OH_NativeXComponent* nativeXComponent);
    
    void SetWidth(uint64_t width) { m_width = width; }
    void SetHeight(uint64_t height) { m_height = height; }
    void SetNativeWindow(OHNativeWindow *nativeWindow) { m_nativeWindow = nativeWindow; }
    
    void Prepare();
    void Create();
    void Draw(NativePixelMap* nativePixelMap);
    void DisPlay();
    void Destroy();
    

private:
    static std::unordered_map<OH_NativeXComponent*, PluginRender*> m_instance;
    OH_NativeXComponent_Callback m_renderCallback;
    OH_NativeXComponent_MouseEvent_Callback m_mouseCallback;
    
    uint64_t m_width = 0;
    uint64_t m_height = 0;
    
    OH_Drawing_Bitmap *m_bitmap = nullptr;
    OH_Drawing_Canvas *m_canvas = nullptr;
    
    OHNativeWindow *m_nativeWindow = nullptr;
    uint32_t *m_mappedAddr = nullptr;
    BufferHandle *m_bufferHandle = nullptr;
    NativeWindowBuffer *m_buffer = nullptr;
    int m_fenceFd = 0;
    
};
} // namespace NativeXComponentSample
#endif // NATIVE_XCOMPONENT_PLUGIN_RENDER_H
